﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Practices.Unity;
using System.ServiceModel.Channels;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace DddCms.Presentation.Utility
{

    /// <summary>
    /// 
    /// </summary>
    public class UnityControllerFactory : IControllerFactory
    {
        private IUnityContainer _container;
        private IControllerFactory _innerFactory;

        /// <summary>
        /// Initializes a new instance of the <see cref="UnityControllerFactory"/> class.
        /// </summary>
        /// <param name="container">The container.</param>
        public UnityControllerFactory(IUnityContainer container)
            : this(container, new DefaultControllerFactory())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UnityControllerFactory"/> class.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <param name="innerFactory">The inner factory.</param>
        protected UnityControllerFactory(IUnityContainer container,
                                         IControllerFactory innerFactory)
        {
            _container = container;
            _innerFactory = innerFactory;
        }


        /// <summary>
        /// Creates the specified controller by using the specified request context.
        /// </summary>
        /// <param name="requestContext">The request context.</param>
        /// <param name="controllerName">The name of the controller.</param>
        /// <returns>
        /// The controller.
        /// </returns>
        public IController CreateController(System.Web.Routing.RequestContext requestContext,
                                            string controllerName)
        {
            try
            {
                return _container.Resolve<IController>(controllerName.ToLowerInvariant());
            }
            catch (Exception ex)
            {
                try
                {
                    return _innerFactory.CreateController(requestContext, controllerName);
                }
                catch (Exception)
                {

                    return null;
                }

            }
        }

        /// <summary>
        /// Releases the specified controller.
        /// </summary>
        /// <param name="controller">The controller.</param>
        public void ReleaseController(IController controller)
        {
            _container.Teardown(controller);
        }


        /// <summary>
        /// Gets the controller's session behavior.
        /// </summary>
        /// <param name="requestContext">The request context.</param>
        /// <param name="controllerName">The name of the controller whose session behavior you want to get.</param>
        /// <returns>
        /// The controller's session behavior.
        /// </returns>
        public System.Web.SessionState.SessionStateBehavior GetControllerSessionBehavior(System.Web.Routing.RequestContext requestContext, string controllerName)
        {
            return _innerFactory.GetControllerSessionBehavior(requestContext, controllerName);
        }
    }

}